# Copyright(C) 2023 InfiniFlow, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

'''
This example is about connecting to the local infinity instance, creating table, inserting data, and searching data
'''

import infinity
import sys

try:
    # Use infinity_embedded module to open a local directory
    # infinity_instance = infinity.connect("/var/infinity")

    #  Use infinity module to connect a remote server
    infinity_instance = infinity.connect(infinity.common.NetworkAddress("127.0.0.1", 23817))

    loop_count = 10000

    for i in range(loop_count):
        print(f"{i} / {loop_count}")
        # 2.1 create database:
        db = infinity_instance.create_database('test_pysdk_test_repeatedly_create_drop_show_databases')

        # 2.2 show database
        dbs = infinity_instance.list_databases()
        res_dbs = []
        for db_name in dbs.db_names:
            if (db_name.startswith("test_pysdk")) or db_name == "default_db":
                assert db_name in ['test_pysdk_test_repeatedly_create_drop_show_databases', "default_db"]
                res_dbs.append(db_name)
        assert len(res_dbs) == 2

        infinity_instance.drop_database('test_pysdk_test_repeatedly_create_drop_show_databases')

    infinity_instance.disconnect()

    print('test done')
    sys.exit(0)
except Exception as e:
    print(str(e))
    sys.exit(-1)
